<template>
  <div class='body'>
    <el-table
      :data="baseList"
      style="width: 100%">
      <el-table-column
        prop="number"
        width="120"
        label="试题编号">
      </el-table-column>
      <el-table-column
        prop="subject"
        width="120"
        label="学科">
      </el-table-column>
      <el-table-column
        prop="catalog"
        width="120"
        label="目录">
      </el-table-column>
      <el-table-column
        width="120"
        label="题型">
        <template slot-scope="{row}">
          <span v-if="row.questionType==='1'">单选</span>
          <span v-if="row.questionType==='2'">多选</span>
          <span v-if="row.questionType==='3'">简答</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="question"
        width="180"
        label="题干">
        <template slot-scope="{row}">
          <span v-html='row.question'></span>
        </template>
      </el-table-column>
      <el-table-column
        label="录入时间"
        width='180'
        >
        <template slot-scope="{row}">
          {{row.addDate|formatTimeXxh}}
        </template>
      </el-table-column>
      <el-table-column
        prop="difficulty"
        width="180"
        label="难度">
        <template slot-scope="{row}">
          <span v-if="row.difficulty==='1'">简单</span>
          <span v-if="row.difficulty==='2'">一般</span>
          <span v-if="row.difficulty==='3'">困难</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="creator"
        width="180"
        label="录入人">
      </el-table-column>
      <el-table-column
        width="180"
        label="审核状态"
        >
        <template slot-scope="{row}">
          <span v-if='row.chkState===0'>待审核</span>
          <span v-if='row.chkState===1'>已审核</span>
          <span v-if='row.chkState===2'>已拒绝</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="chkRemarks"
        width="120"
        label="审核意见">
      </el-table-column>
      <el-table-column
        prop="chkUser"
        width="120"
        label="审核人">
      </el-table-column>
      <el-table-column
        label="发布状态"
        width="120">
        <template slot-scope="{row}">
          <span v-if='row.chkState===1&&row.publishState===1'>已发布</span>
          <span v-if='row.chkState===1&&row.publishState===0'>已下架</span>
          <span v-if='row.chkState===2'>待发布</span>
          <span v-if='row.chkState===0&&row.publishState===0'>待发布</span>
          <span v-if='row.chkState===0&&row.publishState===1'>待发布</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="220"
        fixed="right"
        >
        <template slot-scope="{row}">
          <el-button
            type="text"
            size="mini"
            circle
            @click='preFn(row)'
            >
            预览
            </el-button>
            <el-button type="text" size="mini" :disabled='row.chkState!==0' @click='checkFn(row)'>审核</el-button>
            <el-button type="text" size="mini" @click="$router.push('/questions/new')" :disabled='row.publishState===1' >修改</el-button>
            <el-button type="text" size="mini" v-if='row.publishState===0' @click='goUpandDown(row)'>上架</el-button>
            <el-button type="text" size="mini" v-else @click='goUpandDown(row)'>下架</el-button>
            <el-button type="text" size="mini" @click='delFn(row)' :disabled='row.publishState===1'>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 预览页面弹窗 -->
    <el-dialog
      title="题目预览"
      :visible.sync="preDialogVisible"
      width="60%"
      @close='onClose'
      class='questions-preview'
      >
      <!-- 预览第一行 -->
      <el-row>
        <el-col :span='6'>【题型】:
          <span v-if="preForm.questionType==='1'">单选题</span>
          <span v-if="preForm.questionType==='2'">多选题</span>
          <span v-if="preForm.questionType==='3'">简答题</span>
        </el-col>
        <el-col :span='6'>【编号】: {{preForm.id}}</el-col>
        <el-col :span='6'>【难度】:
          <span v-if="preForm.difficulty==='1'">简单</span>
          <span v-if="preForm.difficulty==='2'">一般</span>
          <span v-if="preForm.difficulty==='3'">困难</span>
        </el-col>
        <el-col :span='6'>【标签】: {{preForm.tags}}</el-col>
        <el-col :span='6'>【学科】: {{preForm.subjectName}}</el-col>
        <el-col :span='6'>【目录】: {{preForm.directoryName}}</el-col>
        <el-col :span='6'>【方向】: {{preForm.direction}}</el-col>
      </el-row>
      <hr>
      <!-- 预览第二行 -->
      <el-row>
        <div>【题干】:</div>
        <span id='showTxt' ref='showTxt'></span>
        <div>
          <span v-if="preForm.questionType==='1'">单选题</span>
          <span v-if="preForm.questionType==='2'">多选题</span>
          <span v-if="preForm.questionType==='2'||preForm.questionType==='1'">选项:（以下选中的选项为正确答案）</span>
        </div>
        <!-- 单选显示 -->
        <div v-if="preForm.questionType==='1'">
          <div style='margin:10px 0' v-for='item in radioList' :key='item.id'>
            <el-radio :value="radio" :label='item.isRight'>{{item.title}}</el-radio>
          </div>
        </div>
        <!-- 多选显示 -->
        <div v-if="preForm.questionType==='2'">
          <div v-for='item in radioList' :key='item.id'>
            <!-- :checked='item.isRight===1?true:false' -->
            <el-checkbox  :value='item.isRight===1?true:false'>{{item.title}}</el-checkbox>
          </div>
        </div>
      </el-row>
      <hr>
      <!-- 预览第三行 -->
      <el-row>
        <div>
          【参考答案】: <span><el-button type="danger" size='mini' @click='isShowVideo=true'>视频答案预览</el-button></span>
        </div>
        <video
        controls="controls"
        :src='preForm.videoURL'
        v-if='isShowVideo'
        autoplay
        ></video>
      </el-row>
      <hr>
      <el-row>
        <div>
          【答案解析】:<div style='display:inline-block' v-html='preForm.answer'></div>
        </div>
      </el-row>
      <hr>
      <el-row>
        <div>
          <span>【题目备注】:</span><span ref='tips'></span>
        </div>
      </el-row>
      <el-button @click="preDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="onClose">关闭</el-button>
    </el-dialog>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="reqForm.page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="reqForm.pagesize"
      layout="prev, pager, next, sizes, jumper"
      :total="total"
      background
      >
    </el-pagination>
    <!-- 审核按钮弹窗 -->
    <el-dialog
      title="题目审核"
      :visible.sync="checkDialogVisible"
      width="30%">
      <div>
        <el-radio v-model.number="checkForm.chkState" :label="1">通过</el-radio>
        <el-radio v-model.number="checkForm.chkState" :label="2">拒绝</el-radio>
      </div>
      <el-input
        type="textarea"
        :rows="2"
        placeholder="请输入内容"
        v-model="checkForm.chkRemarks">
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onClose">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { detail, remove, choiceAdd, choiceCheck, choicePublish } from '@/api/hmmm/questions'
export default {
  name: 'FavorFormBody',
  props: {
    reqForm: {
      type: Object,
      required: true
    },
    baseList: {
      type: Array,
      required: true
    },
    total: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      preDialogVisible: false,
      preForm: {},
      radioList: [],
      radio: 1,
      checkedBoxList: [],
      checkDialogVisible: false,
      checkForm: {
        chkState: 1,
        chkRemarks: '',
        id: 0
      },
      isShowVideo: false
    }
  },

  created () {
  },

  methods: {
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.reqForm.pagesize = val
      this.$emit('loadList')
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.reqForm.page = val
      this.$emit('loadList')
    },
    // 点击预览触发函数
    async preFn (row) {
      console.log(row)
      const res = await detail(row)
      console.log(22222, res.data)
      this.preForm = res.data
      this.preDialogVisible = true
      this.$nextTick(() => {
        // 等待弹窗里面的span dom 加载完成
        this.$refs.showTxt.innerHTML = res.data.question
        this.radioList = res.data.options
        // this.$refs.answer.innerHTML = res.data.answer
        this.$refs.tips.innerHTML = res.data.remarks
      })
      const newArr = []
      res.data.options.forEach(item => {
        if (item.isRight === 1) {
          newArr.push(item.title)
        }
      })
      console.log(newArr)
    },
    // 点击删除图标触发函数
    delFn (row) {
      console.log(row)
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.$emit('loadList')
      }).catch(() => {
      })
    },
    // 点击勾添加精选
    async addFav (row) {
      console.log(row)
      const newObj = JSON.parse(JSON.stringify(row))
      newObj.choiceState = 1

      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(async () => {
        await choiceAdd(newObj)
        this.$message({
          type: 'success',
          message: '加入精选成功!'
        })
        this.$emit('loadList')
      }).catch(() => {
      })
    },
    // 点击审核功能
    checkFn (row) {
      console.log(row.id)
      this.checkForm.id = row.id
      this.checkDialogVisible = true
    },
    // 点击确认审核
    async checkConfirm (row) {
      console.log(333, row)
      await choiceCheck(this.checkForm)
      this.checkDialogVisible = false
      this.$emit('loadList')
    },
    // 上架下架
    async goUpandDown (row) {
      console.log(333, row)
      const newObj = JSON.parse(JSON.stringify(row))
      if (row.publishState === 1) {
        newObj.publishState = 0
        await this.$confirm('您确认下架这道题目吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await choicePublish(newObj)
          this.$message({
            type: 'success',
            message: '下架成功!'
          })
          this.$emit('loadList')
        })
      } else {
        newObj.publishState = 1
        await this.$confirm('您确认上架这道题目吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await choicePublish(newObj)
          this.$message({
            type: 'success',
            message: '上架成功!'
          })
          this.$emit('loadList')
        })
      }
    },
    // 点击关闭的时候触发的事件
    onClose () {
      this.preDialogVisible = false
      this.isShowVideo = false
    }
  }
}
</script>

<style scoped lang='less'>
#showTxt{
  color:blue
}
.dialog-footer{
  display: flex;
  justify-content: end;
}

.questions-preview .el-col{
  padding: 10px 0;
}
</style>
